package database

import (
	// "fmt"

	"gorm.io/gorm"
	"time"
)

type Category struct {
	ID       int64  `gorm:"column:id"` // 主键
	Category string `gorm:"column:category"`
	Comment  string `gorm:"column:comment"`
}

type Item struct {
	ID    int64  `gorm:"column:id"` // 主键
	Title string `gorm:"column:title"`
	Price string `gorm:"column:price"`
	// Category Category `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
	Category int64  `gorm:"column:category"`
	Detail   string `gorm:"column:detail"`
	UserID   int64  `gorm:"column:user_id"` // 新增，物品归属用户
	Image    string `gorm:"column:image"`   // 新增图片字段
}

type User struct {
	ID       int64  `gorm:"column:id"`
	Username string `gorm:"column:username"`
	Email    string `gorm:"column:email"`
	Password string `gorm:"column:password"`
}
type User_statues struct {
	UserID       int64   `gorm:"column:user_id"`
	Username     string  `gorm:"column:username"`
	UserImg      *string `gorm:"column:user_image"`
	Intro        *string `gorm:"column:intro"`
	Gender       *string `gorm:"column:gender"`
	Birthday     *string `gorm:"column:birthday"` //在未设置生日时，该字段为 nil，GORM 插入数据库时就会变成 NULL，而不是 ''。
	Prefer       *string `gorm:"column:prefer"`
	University   *string `gorm:"column:university"`
	Zhima_Credit *string `gorm:"column:zhima_credit"`
}
type End struct {
	ID           int64  `gorm:"column:id"`
	UserID       int64  `gorm:"column:user_id"`
	Shopname     string `gorm:"column:shopname"`
	Type         string `gorm:"column:type"`
	Number       int64  `gorm:"column:number"`
	Money        int64  `gorm:"column:money"`
	Shoppingtime string `gorm:"column:shoppingtime"`
}
type Cart struct {
	ID       int64     `gorm:"column:id"`
	UserID   int64     `gorm:"column:user_id"`
	ItemID   int64     `gorm:"column:item_id"`
	Quantity int       `gorm:"column:quantity"`
	AddTime  time.Time `gorm:"column:added_time"`
}

func (c Category) TableName() string {
	return "category"
}

func (i Item) TableName() string {
	return "item"
}

func (User) TableName() string {
	return "user"
}
func (End) TableName() string {
	return "end"
}
func (c Cart) TableName() string {
	return "cart"
}

func Insert(db *gorm.DB, item Item) Item {
	db.Create(&item)
	return item
}

func Delete() {}

func Update() {}

func QueryItem(db *gorm.DB, id string) []string {
	//sql := "SELECT * FROM `item` LIMIT 10"
	//sql := "SELECT price FROM `item` WHERE id = " + id + " LIMIT 10"
	sql := "SELECT price FROM `item` WHERE id = ? LIMIT 10"
	results := make([]string, 10)
	//db.Raw(sql).Scan(&results)
	db.Raw(sql, id).Scan(&results)
	// fmt.Println(results[0])
	return results
}

func QueryCategory(db *gorm.DB) []Category {
	sql := "SELECT * FROM `category` LIMIT 10"
	results := make([]Category, 10)
	db.Raw(sql).Scan(&results)
	// fmt.Println(results[0])
	return results
}
func QueryPrice(db *gorm.DB, price string) []Item {
	sql := "SELECT * FROM `item` WHERE price > ?"
	results := make([]Item, 10) // LIMIT 10
	db.Raw(sql, price).Scan(&results)
	// fmt.Println(results[0])
	return results
}
func GetFood(db *gorm.DB) []Item {
	sql := "SELECT * FROM `item` WHERE category = 1"
	results := make([]Item, 10)
	db.Raw(sql).Scan(&results)
	// fmt.Println(results[0])
	return results
}
func GetComment(db *gorm.DB) []Category {
	sql := "SELECT comment FROM `category` WHERE id = 1"
	results := make([]Category, 10)
	db.Raw(sql).Scan(&results)
	// fmt.Println(results[0])
	return results
}

// 查询某用户的所有物品
func QueryItemsByUser(db *gorm.DB, userID int64) []Item {
	var items []Item
	db.Where("user_id = ?", userID).Find(&items)
	return items
}

// 按关键字搜索物品（标题或详情模糊匹配）
func SearchItems(db *gorm.DB, keyword string) []Item {
	var items []Item
	like := "%" + keyword + "%"
	db.Where("title LIKE ? OR detail LIKE ?", like, like).Find(&items)
	return items
}

// 删除指定ID且属于指定用户的物品
func DeleteItemByID(db *gorm.DB, id int64, userID int64) error {
	return db.Where("id = ? AND user_id = ?", id, userID).Delete(&Item{}).Error
}

// 更新指定ID且属于指定用户的物品
func UpdateItemByID(db *gorm.DB, id int64, userID int64, title, price string, category int64, detail string) error {
	return db.Model(&Item{}).Where("id = ? AND user_id = ?", id, userID).Updates(Item{
		Title:    title,
		Price:    price,
		Category: category,
		Detail:   detail,
	}).Error
}

// 查询单个物品
func GetItemByID(db *gorm.DB, id int64, userID int64) (Item, error) {
	var item Item
	err := db.Where("id = ? AND user_id = ?", id, userID).First(&item).Error
	return item, err
}
